/*
 * FinanceApp.java
 *
 *  
 */
package org.msb.finance.app;

import java.awt.EventQueue;
import java.awt.Frame;
import java.util.HashSet;
import java.util.ResourceBundle;
import java.util.Set;

import org.msb.finance.data.Cabinet;

/**
 * 
 * 
 * @author Marc Boudreau
 * 
 */
public class FinanceApp {

	/*
	 * 
	 */
	private static FinanceApp instance = new FinanceApp();

	/*
	 * 
	 */
	private Resources messageResources;

	/*
	 * 
	 */
	private FinanceFrame frame;

	/*
	 * 
	 */
	private CabinetManager cabinetManager;

	/*
	 * 
	 */
	private Set<FinanceFileChangeListener> financeFileListeners;

	/*
	 * 
	 */
	private FinanceApp() {
		this.messageResources = new Resources(ResourceBundle.getBundle("org/msb/finance/res/messages")); //$NON-NLS-1$
		this.financeFileListeners = new HashSet<FinanceFileChangeListener>();

		assert this.messageResources != null : "Failed to load ResourceBundle containing application messages."; //$NON-NLS-1$
	}

	/*
	 * 
	 */
	static Frame getFrame() {
		return instance.frame;
	}

	/**
	 * 
	 * @param manager
	 */
	public static void setCabinetManager(CabinetManager manager) {
		if (null != instance.cabinetManager) {
			instance.cabinetManager.close();
		}

		instance.cabinetManager = manager;

		for (FinanceFileChangeListener listener : instance.financeFileListeners) {
			listener.financeFileChanged(manager);
		}
	}

	/**
	 * 
	 * @return s
	 */
	public static CabinetManager getCabinetManager() {
		return instance.cabinetManager;
	}

	/*
	 * 
	 */
	static void addFinanceFileChangeListener(FinanceFileChangeListener listener) {
		instance.financeFileListeners.add(listener);
	}

	/**
	 * 
	 * @return s
	 */
	public static Resources getMessageResources() {
		return instance.messageResources;
	}

	/**
	 * 
	 * @return s
	 */
	public static Cabinet getCabinet() {
		return instance.cabinetManager.getCabinet();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				instance.frame = new FinanceFrame();
				instance.frame.setVisible(true);
			}
		});
	}

}
